<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
   <style>
       #loginFormDiv {
           border: 1px solid blue;
           margin: 100px auto;
           padding: 50px 20px 20px 50px ;
           width: 600px;
           height: 300px;
           box-shadow: 15px 15px 15px 15px #888888;
       }
   </style>
</head>
<body>

<div id="loginFormDiv">
<!-- 你的 HTML 代码 -->
<form class="layui-form" action="" lay-filter="example">

    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码框</label>
        <div class="layui-input-block">
            <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否免登录</label>
        <div class="layui-input-block">
            <input type="checkbox" name="autologin" lay-skin="switch" lay-text="ON|OFF">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">验证码</label>
        <div class="layui-input-inline">
            <input type="text" name="capchar" autocomplete="off" placeholder="请输入验证码" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <img src="/hutoolcap" style="width:85px;height: 38px" id="cap"/>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>
</div>
<script src="./layui/layui.js"></script>
<script>
    layui.use( function(){
        var layer = layui.layer
            ,form = layui.form;

        var $ = layui.$;


        // 注册图片的单击事件
        $("#cap").click(function () {
            path = "/hutoolcap?"+new Date().getTime();
            $(this).attr("src",path);
        });


        //监听表单提交
        form.on('submit(loginBtn)', function(data){

            // 发ajax请求
            $.ajax({
                url:"/user/login",
                data:data.field,   // 对应的post请求体中的数据格式为 username=luffy&password=123456&capchar=LH
                // 如果请求体中要放json，使用如下两行代码
                // contentType:"application/json",
                // data:JSON.stringify(data.field),
                type:"post",
                success: function(result){
                    console.log(result);
                    // 登录成功进行页面跳转
                    if(result.code == "1"){
                        window.location = "/plateform.html";
                    }else{
                        // 登录失败给出提示信息
                        layer.alert(result.message, {
                            title: '提示信息'
                        });
                    }
                },
                error:function (result) {
                    console.log(result);
                    // 登录失败给出提示信息
                    layer.alert(result.message, {
                        title: '提示信息'
                    });
                }
            });


            // 注意，这里要return  false ，取消表单提交的默认行为 （往action对应的url发送请求 ）
            return false;
        });

        // layer.msg('Hello World');
    });
</script>


</body>
</html>
